﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Xml;

namespace Shoozla.DataModel
{
    public class XMLCoverStorage : ICoverStorage
    {
        private XmlDocument _doc;
        private XmlElement _root;

        public XMLCoverStorage()
        {
            Reset();
        }

        public void Reset() {
            _doc = new XmlDocument();
            _root = _doc.CreateElement("shoozla");
            _doc.AppendChild(_root);
        }

        public void AddCoverToStorage(Mp3File c) { 
            XmlElement coverEl = _doc.CreateElement("cover");
            coverEl.SetAttribute("filehash", c.FileName);
            coverEl.SetAttribute("uri", c.ImageUrl.ToString());
            _root.AppendChild(coverEl);
        }

        public void LoadFromStorage(string filename) {
            try
            {
                _doc.Load(filename);
            }
            catch(Exception e)
            {
                Trace.WriteLine(String.Format("[ERROR] XMLCoverStorage: LoadFromStorage: " + e.Message));
            }
        }

        public void SaveToStorage(string filename) {

            try
            {
                _doc.Save(filename); 
            }
            catch (Exception e)
            {
                Trace.WriteLine(String.Format("[ERROR] XMLCoverStorage: SaveToStorage: " + e.Message));
            } 
        }

        public MultithreadingObservableCollection<Mp3File> GetAllCovers()
        {
            //MultithreadingObservableCollection<Mp3File> covers = new MultithreadingObservableCollection<Mp3File>();
            //foreach(XmlNode n in _doc.SelectNodes("shoozla/cover"))
            //{
            //    covers.Add(new Mp3File(new Uri(n.Attributes["uri"].Value),n.Attributes["filehash"].Value));
            //}
            //return covers;
            return null;
        }
    }
}
